package com.securitydemo.demo.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 创建Spring Security过滤器
 * @author hyf
 * @Title: ${file_name}
 * @Package com.securitydemo.demo.conf
 * @Description: ${todo}
 * @date 2018/5/10下午11:12
 */

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        System.out.println("前配置");
        http.authorizeRequests()

                //可以直接访问
                .antMatchers("/css/**", "/index","/logoutSuccess").permitAll()
                //必须有user权限
                .antMatchers("/user/**").hasRole("USER")
                .and()
                .logout().logoutSuccessUrl("/logoutSuccess")
                //配置  logoutSuccessHandler  logoutSuccessUrl()的设置会被忽略
                .logoutSuccessHandler(new LogoutSuccessHandler() {
                    @Override
                    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
                        System.out.println("onLogoutSuccess触发了");
                    }
                })
                .addLogoutHandler(new LogoutHandler() {
                    @Override
                    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
                        System.out.println("logout触发了");
                    }
                })
                // 表单验证
                .and().formLogin()
                // 登录页面  当登录时，先判断用户是否登录，未登录这访问login3页面.当为post请求时，执行验证用户名方法,而不执行Controller中的post方法
                .loginPage("/login3")
                //登录失败页面
                .failureUrl("/login-error")
                .and().csrf().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }


    @Override
    public void configure(WebSecurity web) throws Exception {
        super.configure(web);
        System.out.println("后配置？");
    }
}